const User = require('../models/user');
const md5 = require('md5');

// 添加用户
<<<<<<< HEAD
exports.addUser=async function(params){
    // 密码加密
    params.password=md5(params.password);
    const result=await User.create(params);
    return result
}
// 注销用户
exports.delUser=async function(params){
    params.password=md5(params.password);
    const result=await User.destroy({
        where:{
            ...params
        }
    });
    return result
}
//修改用户密码
exports.updateUser=async function(username,params){
    params.password=md5(params.password);
    const result = await User.update(params,{
        where:{
            username
        }
    })
    return result
}
// 查询用户
exports.findUser=async function (username,password=-1){
    let res1,res2;
    res1=await User.findAll({
        where:{ 
            username
        }
    })
    if(password!=-1){
        password=md5(password);
        res2=await User.findAll({
            where:{
=======
exports.addUser = async function(params) {
        // 密码加密
        params.password = md5(params.password);
        const result = await User.create(params);
        return result
    }
    // 注销用户
exports.delUser = async function(params) {
        params.password = md5(params.password);
        const result = await User.destroy({
            where: {
                ...params
            }
        });
        return result
    }
    //修改用户密码
exports.updateUser = async function(username, params) {
        params.password = md5(params.password);
        const result = await User.update(params, {
            where: {
                username
            }
        })
        return result
    }
    // 查询用户
exports.findUser = async function(username, password = -1) {
    let res1, res2;
    console.log(username, password)
    res1 = await User.findAll({
        where: {
            username
        }
    })
    console.log(res1)
    if (password != -1) {
        password = md5(password);
        res2 = await User.findAll({
            where: {
>>>>>>> 001
                username,
                password
            }
        })
    }
    return { res1, res2 }
}

//用户查询商家支持的状态数据
exports.businessSupport = async function(username) {
        let [result] = await User.findAll({
            attributes: ['business'],
            where: {
                username
            }
        })
        return result
    }
    //用户添加商家支持操作
exports.businessSupportUpdate = async function(username, params) {
    const [result] = await User.update({ business: params }, {
        attributes: ['business'],
        where: {
            username
        }
    })
    return result
}